<template>
  <div>
    <el-card class="box-card">
      <el-row>
        <el-col>
          <el-button
            class="btn-add"
            @click="handleUpdateProduct()"
            size="mini">
            添加
          </el-button>
          <el-input
            placeholder="请输入内容"
            prefix-icon="el-icon-search"
            v-model="input2">
          </el-input>
        </el-col>
      </el-row>


    </el-card>

    <el-table ref="productTable"
              style="width: 100%"
              :data="list"
              border>
      <el-table-column type="selection" width="60" align="center"></el-table-column>
      <el-table-column label="任务编号" width="100" prop="taskId" align="center">
      </el-table-column>
      <el-table-column label="任务名称" width="200" prop="taskName" align="center">
      </el-table-column>
      <el-table-column label="任务名称" width="400" prop="taskSql" align="center">
      </el-table-column>
      <el-table-column label="执行时间" width="170" prop="taskTime" align="center" :formatter="dateFormatter">
      </el-table-column>
      <el-table-column label="任务类型" width="100" prop="taskType" align="center">
      </el-table-column>
      <el-table-column label="任务状态" width="100" prop="taskStatus" align="center">
      </el-table-column>
      <el-table-column label="创建人" width="100" prop="createPersonName" align="center">
      </el-table-column>
      <el-table-column label="创建时间" width="100" :formatter="dateFormatter" prop="createTime" align="center">
      </el-table-column>
      <el-table-column label="更新人" width="100" prop="updatePersonName" align="center">
      </el-table-column>
      <el-table-column label="更新时间" width="100" :formatter="dateFormatter" prop="updateTime" align="center">
      </el-table-column>
      <el-table-column label="操作" width="160" align="center">
        <template slot-scope="scope">
          <p>
            <el-button
              size="mini"
              @click="handleUpdateProduct(scope.$index, scope.row)">编辑
            </el-button>
          </p>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[5, 10, 20, 40]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="this.page.total"
      :current-page="this.page.page">
    </el-pagination>

    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form ref="filterTable" :model="taskForm" label-width="120px">
        <el-form-item label="任务编号" prop="taskId" v-show="false">
          <el-input v-model="taskForm.taskId" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="taskForm.taskName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="sql语句" prop="taskSql">
          <el-input type="textarea" v-model="taskForm.taskSql" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="执行时间" prop="dataTime">
          <el-date-picker
            v-model="taskForm.taskTime"
            :picker-options="this.pickerOptions"
            type="datetime"
            placeholder="选择日期时间"
            format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务类型" prop="taskType">
          <el-select v-model="taskForm.taskType" placeholder="请选择活动区域">
            <el-option label="暂不执行" value="-1"></el-option>
            <el-option label="执行一次" value="0"></el-option>
            <el-option label="循环执行" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建人" prop="createBY" v-show="false">
          <el-input v-model="taskForm.createBy" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  import moment from "moment"

  export default {
    name: "task",
    data () {
      return {
        list: [],
        taskForm: {
          taskId: "",
          taskName: "",
          taskTime: "",
          taskSql: "",
          taskType: "",
          createBy: 1
        },
        dialogTitle: "",
        dialogVisible: false,
        pickerOptions: {
          disabledDate (time) {
            return time.getTime() < Date.now() - 8.64e7   //禁用以前的日期，今天不禁用
          }
        },
        page: {
          page: 1,
          rows: 10,
          total: 1
        }
      }
    },
    created () {
      this.getList()
    },
    methods: {
      handleConfirm () {
        if (this.dialogTitle == "添加") {
          console.log(this.taskForm)
          this.$axios({
            method: "post",
            url: "http://localhost:7501/task",
            headers: {
              "Content-Type": "application/json"
            },
            data: {
              taskName: this.taskForm.taskName,
              taskTime: moment(this.taskForm.taskTime).format("YYYY-MM-DD HH:mm:ss"),
              taskSql: this.taskForm.taskSql,
              taskType: this.taskForm.taskType,
              taskId: this.taskForm.taskId,
              createBy: this.taskForm.createBy
            }
          }).then(result => {
            if (result.data == true) {
              this.$message.success("添加任务成功")
              this.dialogVisible = false
            } else {
              this.$message.error("添加任务失败，服务器内部错误请检查")
            }
            this.getList()
          })
        } else if (this.dialogTitle == "编辑") {
          console.log(this.taskForm)
          this.$axios({
            method: "put",
            url: "http://localhost:7501/task",
            data: {
              taskId: this.taskForm.taskId,
              taskName: this.taskForm.taskName,
              taskTime: moment(this.taskForm.taskTime).format("YYYY-MM-DD HH:mm:ss"),
              taskSql: this.taskForm.taskSql,
              taskType: this.taskForm.taskType,
              createBy: this.taskForm.createBy
            }
          }).then(result => {
            if (result.data == true) {
              this.$message.success("添加修改成功")
              this.dialogVisible = false
            } else {
              this.$message.error("修改任务失败，服务器内部错误请检查")
            }
            this.getList()
          })
        }
      },
      dateFormatter (row, column) {
        var date = row[column.property]
        if (date == undefined) {
          return ""
        }

        return moment(date).format("YYYY-MM-DD HH:mm:ss")
      },
      getList () {
        this.$axios.get("http://localhost:7501/task/andAdmin?page=" + this.page.page + "&rows=" + this.page.rows).then(result => {
          console.log(result)
          this.page.total = result.data.total
          this.list = result.data.list
        })
      },
      /*删除*/
      handleDelete (index, row) {
        var brandStatus = row.brandStatus == 1 ? 1 : 0
        console.log(brandStatus)
        this.$axios({
          method: "post",
          url: "http://localhost:7501/task",
          params: {
            spuBrandId: row.spuBrandId,
            brandStatus: brandStatus,
            updateBy: 1
          }
        }).then(result => {
          this.getList()
        })
      },
      /*编辑*/
      handleUpdateProduct (index, row) {
        this.dialogVisible = true
        if (row == undefined || row == "") {
          this.dialogTitle = "添加"
          console.log(row)
          this.taskForm.taskName = "",
            this.taskForm.taskTime = "",
            this.taskForm.taskSql = "",
            this.taskForm.taskType = "",
            this.taskForm.taskId = ""
        } else {
          this.dialogTitle = "编辑"
          console.log(row)
          this.taskForm.taskId = row.taskId,
            this.taskForm.taskName = row.taskName,
            this.taskForm.taskTime = row.taskTime,
            this.taskForm.taskSql = row.taskSql,
            this.taskForm.createBy = row.createBy
          // if (rowtaskType == -1) {
            this.taskForm.taskType = row.taskType
          // } else if (rowtaskType == 0) {
          // } else if (rowtaskType == 1) {
          // }
        }
      },
      /*查看单条*/
      handleShowProduct (index, row) {
        console.log("handleShowBrand", row)
      },
      handleSizeChange: function (rows) {
        this.page.rows = rows
        this.getList()
      },
      handleCurrentChange: function (page) {
        this.page.page = page
        this.getList()
      },
    }
  }
</script>

<style scoped>

</style>
